--2. Write a function splitWith that acts similarly to words but takes a predicate and a
--list of any type, and then splits its input list on every element for which the predicate
--returns False:
---- file: ch04/ch04.exercises.hs
--splitWith :: (a -> Bool) -> [a] -> [[a]]

splitWith :: (a -> Bool) -> [a] -> [[a]]
splitWith _ [] = []
--splitWith predicate list = let (pre, suf) = span predicate list
--                               (_, sufWithSepRemoved) = break predicate suf
--                           in pre : splitWith predicate sufWithSepRemoved
-- 无法处理前缀空格
-- splitWith isLetter "   ab  cd  ef"
-- ["","ab","cd","ef"]

--splitWith predicate list = let (_, suf) = break predicate list
--                               (sufpre, sufsuf) = span predicate suf
--                           in sufpre : splitWith predicate sufsuf
-- 无法处理后缀空格
-- splitWith isLetter "ab  cd  ef  "
-- ["ab","cd","ef",""]

splitWith predicate list = let (_, suf) = break predicate list
                               (sufpre, sufsuf) = span predicate suf
                           in 
                             if null sufpre
                             then splitWith predicate sufsuf
                             else sufpre : splitWith predicate sufsuf
